<template>
  <div class="tab-container">
    <el-tabs v-model="activeName" style="margin-top:15px;" type="border-card">
      <el-tab-pane v-for="item in tabMapOptions" :key="item.key" :label="item.label" :name="item.key" >
        <keep-alive>
          <tab-pane v-if="activeName==item.key" :type="item.key"  :list ="list"@create="showCreatedTimes" />
        </keep-alive>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import TabPane from './components/TabPane'

import table_data from './components/table_data.vue'

export default {
  name: 'Tab',
  components: { TabPane, table_data },
  data() {
    return {
      tabMapOptions: [
        { label: 'China', key: 'CN' },
        { label: 'USA', key: 'US' },
        { label: 'Japan', key: 'JP' },
        { label: 'Eurozone', key: 'EU' }
      ],
      activeName: 'CN',
      createdTimes: 0,
      taskArray: [],
      list : []
    }
  },
  mounted(){
    var myHeaders = new Headers();
      myHeaders.append("User-Agent", "Apifox/1.0.0 (https://apifox.com)");
      myHeaders.append("Authorization", "Bearer "+sessionStorage.getItem('token'))
      var requestOptions = {
        method: 'GET',
        headers: myHeaders,
        redirect: 'follow'
      };

    fetch("http://1.92.204.101:5000/task/get_user_statement", requestOptions)
      .then(response => response.text())
      .then(result => {
        var data = JSON.parse(result)
          this.taskArray = data;
          this.list = this.taskArray.map(item => {
            return {
              id: item.uid,
              timestamp: item.assigned === 0 ? null :item.starttime,
              title: item.assigned === 0 ? null :item.task_placex + item.task_placey,
              author: item.name,
              pageviews: item.assigned
            }
          });
          console.log(data[0])
          console.log(this.taskArray[0])
          console.log(this.list[0])
      })
      .catch(error => console.log('error', error));
  },
  watch: {
    activeName(val) {
      this.$router.push(`${this.$route.path}?tab=${val}`)
    }
  },
  created() {
    // init the default selected tab
    const tab = this.$route.query.tab
    if (tab) {
      this.activeName = tab
    }
  },
  methods: {
    showCreatedTimes() {
      this.createdTimes = this.createdTimes + 1
    },
  }
}
</script>

<style scoped>
  .tab-container {
    margin: 30px;
  }
</style>
